package cn.xo68.boot.auth.server.web.api;

import cn.xo68.boot.auth.core.common.AuthConstants;

import cn.xo68.boot.auth.core.domain.Oauth2Principal;
import cn.xo68.boot.auth.server.domain.OauthUser;
import cn.xo68.boot.auth.server.domain.OauthUserRole;
import cn.xo68.boot.auth.server.domain.dto.OauthUserDto;
import cn.xo68.boot.auth.server.service.OauthUserRoleService;
import cn.xo68.core.date.DateTime;
import cn.xo68.core.util.StringTools;
import cn.xo68.core.util.UUIDUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.xo68.boot.auth.server.service.OauthUserService;

import java.util.List;


/**
 * OauthUser Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年09月10日 11:34:33
 *
 */
@RequestMapping("/api/OauthUser")
@RestController
public class OauthUserController{
    @Autowired
    private OauthUserService oauthUserService;
    @Autowired
    private OauthUserRoleService oauthUserRoleService;

	@GetMapping("/find")
    public OauthUserDto find(@RequestParam(name="id") String id) {
        OauthUser user= oauthUserService.getByUserId(id);
        List<String> roles = oauthUserRoleService.getUserRoles(id);
        OauthUserDto userDto=new OauthUserDto();

        BeanUtils.copyProperties(user, userDto,"");
        userDto.setRoleIds(roles);

        return userDto;
    }
    
    @GetMapping("/list")
    public PageInfo<OauthUser> list(@RequestParam(name="pageIndex",defaultValue="1") int pageIndex, @RequestParam(name="pageSize",defaultValue="10") int pageSize){
        return oauthUserService.list(pageIndex, pageSize);
    }
	
	@PostMapping("/add")
    public String add(@RequestBody OauthUserDto dto , Subject subject, Oauth2Principal oauth2Principal) {
	    OauthUser entity=dto.buildUser(oauth2Principal.getName(),oauth2Principal.getUserId());
        if(StringTools.isEmpty(entity.getUserId())){
            entity.setUserId(UUIDUtils.createUUId());
        }
        entity.setCreateTime(DateTime.Now().getDate());
        entity.setUserLock(0);
        List<OauthUserRole> roles = dto.buildRoleList(entity.getUserId(), oauth2Principal.getName(), oauth2Principal.getUserId());
        oauthUserService.insert(entity, roles);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
    }
	
	@PostMapping("/delete")
	public String delete(@RequestParam(name="id") String id) {
	    oauthUserService.delete(id);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}
	
	@PostMapping("/update")
	public String update(@RequestBody OauthUserDto dto , Subject subject, Oauth2Principal oauth2Principal) {
        OauthUser entity=dto.buildUser(oauth2Principal.getName(),oauth2Principal.getUserId());
        List<OauthUserRole> roles = dto.buildRoleList(entity.getUserId(), oauth2Principal.getName(), oauth2Principal.getUserId());
	    oauthUserService.update(entity,roles);
        return AuthConstants.RESPONSE_DATA_SUCCESS;
	}
}